

*** Set up directories

global data ** folder with data **
global tables ** folder for tables **
global figures ** folder for figures **


set more off
	
clear all
cap log close
log using 2reg_firststage.log , replace

set matsize 10000
	
eststo clear

cd $data
use firststage.dta

/***********************************************
PREPARATION FOR REGRESSIONS
***********************************************/

** Make globals with 3-way interaction between education, race, and state; two-way interactions between ed-year, race-year, and state-year; and race-state trends for inclusion in regressions
**Notes: 
	*** Variables e1-e* are dummies for education, r1-r* are dummies for race, s1-s51 are dummies for state, ///
	*** Variables  e`e's`s'r`r' are interactions between all combinations of e, s, and r dummies
	*** Variables
	
	tab educ, g(e)
		local maxed =r(r)
	tab race, g(r)
		local maxrace =r(r)
	tab fipstate, g(s)
		local maxstate= r(r)
	tab year, g(y)
		local maxyear= r(r)
	
	//3-WAY INTERACTION: STATE*ED*RACE

	global threeway ""

	forvalues e = 1/`maxed' {
	forvalues s = 1/`maxstate' {
	forvalues r = 1/`maxrace' {
			
		global threeway $threeway e`e's`s'r`r'
		
	}
	}
	}

	
	global racestatetrend ""
	
	forvalues s = 1/`maxstate' {
	forvalues r = 1/`maxrace' {
			
		global racestatetrend $racestatetrend s`s'r`r'year
		
	}
	}
	

	global educyear ""
	
	forvalues e = 1/`maxed' {
	forvalues y = 1/`maxyear' {
			
		global educyear $educyear e`e'y`y'
		
	}
	}

	global raceyear ""
	
	forvalues r = 1/`maxrace' {
	forvalues y = 1/`maxyear' {
			
		global raceyear $raceyear r`r'y`y'
		
	}
	}	
	
	global stateyear ""
	
	forvalues s = 1/`maxstate' {
	forvalues y = 1/`maxyear' {
			
		global stateyear $stateyear s`s'y`y'
		
	}
	}		
	

/*****************************************************
	FIGURE 5
*****************************************************/
		
	foreach g in male fem rel {

	
	if "`g'" == "male"	local ytitle = "Ln Male Wage"
	if "`g'" == "fem" 	local ytitle = "Ln Female Wage"
	if "`g'" == "rel"  	local ytitle = "Ln Relative Wage"

	preserve 
	keep if year ==1980 |year ==2010
	
	gen consistentyear = 1 if year == 1980 
	replace consistentyear = 2 if year == 2010
	
	xtset uniqueid consistentyear 
		
	if "`g'" != "rel" gen dflncpsshift`g'wage = lncps`g'shiftwage - L.lncps`g'shiftwage
	if "`g'" == "rel" gen dflncpsshift`g'wage = lncpsshift`g'wage - L.lncpsshift`g'wage
	
	gen dflncps`g'wage = lncps`g'wage - L.lncps`g'wage

	//CORRELATION BETWEEN CHANGE IN WAGES OVER TIME
	if "`g'" != "rel"  scatter dflncps`g'wage dflncpsshift`g'wage /// 
		[aweight=cps_pop], msize(*0.4) mcolor(black) msize(tiny) /// 
		ytitle("Change in Observed `ytitle' 1980-2010", size(medium)) xtitle("Change in Potential `ytitle' 1980-2010", size(medium)) /// 
		fcolor(white) graphregion(fcolor(white)) scheme(s1mono) legend(off) || ///
		lfit  dflncps`g'wage dflncpsshift`g'wage  /// 
		[aweight=cps_pop], clcolor(black) lpattern(dash) legend(off) 
				
	if "`g'" == "rel"  scatter dflncps`g'wage dflncpsshift`g'wage if dflncps`g'wage>=-.25 & dflncps`g'wage<= .5 & dflncpsshift`g'wage>=-.1 & dflncpsshift`g'wage<=.2 /// 
		[aweight=cps_pop], msize(*0.4) mcolor( black) msize(tiny) /// 
		ytitle("Change in Observed `ytitle' 1980-2010", size(medium)) xtitle("Change in Potential `ytitle' 1980-2010", size(medium)) /// 
		fcolor(white) graphregion(fcolor(white)) scheme(s1mono) legend(off) || ///
		lfit  dflncps`g'wage dflncpsshift`g'wage  if dflncps`g'wage>=-.25 & dflncps`g'wage<= .5 & dflncpsshift`g'wage>=-.1 & dflncpsshift`g'wage<=.2  /// 
		[aweight=cps_pop], clcolor(black) lpattern(dash) legend(off) 
	
	if "`g'" == "rel" local out 4a
	if "`g'" == "male" local out 4c
	if "`g'" == "fem" local out 4b
	
	cd $figures	
	graph export "$figures/figure`out'.eps"	, replace
	!ps2pdf -dEPSCrop "$figures/figure`out'.eps"	
	!rm -f "$figures/figure`out'.eps"
	cd $dofiles
	
	restore
	
	}	
	

/******************************************
	TABLE 1
******************************************/
	
	eststo clear
	
	local educyear = "$educyear"
	local raceyear = "$raceyear"
	local stateyear =  "$stateyear"
	local fe = "$threeway"	
	
	xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
		`educyear'  `raceyear' `stateyear' 	/// 
		`fe'  /// 		
		yrsedfem yrsedmale sexratio $racestatetrend  ///				
		[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear' yrsedfem yrsedmale sexratio $racestatetrend )

		local partialr2 = e(r2)
		local fstat = e(F)
			
		
	eststo: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
			`educyear'  `raceyear' `stateyear' 	/// 
			`fe'  /// 		
			yrsedfem yrsedmale sexratio $racestatetrend ///				
			[aweight=cps_pop], cluster(fipstate) 
			estadd scalar fstat =  `fstat'		
			estadd scalar partialr2 = `partialr2'

		
	foreach g in fem male {
		
		local educyear = "$educyear"
		local raceyear = "$raceyear"
		local stateyear =  "$stateyear"
		local fe = "$threeway"
			
		
		 xi: ivreg2 lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear' `educyear' `stateyear'  /// 
					`fe'  /// 
					yrsed`g' sexratio $racestatetrend ///
					[aweight=pop`g'],  partial(i.race_num i.educ_num i.year i.fipstate `raceyear' `educyear' `stateyear'  `fe' yrsed`g'  sexratio $racestatetrend)  cluster(fipstate)

		local partialr2 = e(r2)
		local fstat=e(F)
		
		eststo: xi: reg lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear' `educyear' `stateyear'  /// 
					`fe'  /// 
					 yrsed`g' sexratio $racestatetrend    ///
					[aweight=pop`g'],   cluster(fipstate)				
		estadd scalar fstat = `fstat'
		estadd scalar partialr2 = `partialr2'
	
	}
						

	eststo: xi: reg lncpsfemwage lncpsfemshiftwage lncpsmaleshiftwage i.race_num i.educ_num i.year i.fipstate /// 
							i.educ_num*i.year i.race_num*i.year i.fipstate*i.year /// 
							$threeway /// 
							 yrsedfem sexratio $racestatetrend  ///
							[aweight=popfem],   cluster(fipstate)

	eststo: xi: reg lncpsmalewage lncpsfemshiftwage lncpsmaleshiftwage i.race_num i.educ_num i.year i.fipstate /// 
							i.educ_num*i.year i.race_num*i.year i.fipstate*i.year $racestatetrend  /// 
							$threeway /// 
							 yrsedmale sexratio   ///
							[aweight=popmale],   cluster(fipstate)		
					
					
	esttab  using "${tables}/table1.tex", /// 
		replace keep(*wage* ) ///
		se r2 se(3) b(3)  star (* .10 ** .05 *** .01) nonum nonotes noconstant ///
		stat(partialr2 N , /// 
		labels( "Partial R-Squared" "Obs" ) /// 	
		fmt(3 0 0 0 0 0)) mtitle("Relative" "Female" "Male" "Female" "Male") label ///
		mgroups("Corr. w/ Actual" "Cross-Effects?", pattern(1 0 0 1 0 ) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
	

/************************************************************
TABLE A4 - PANEL A
*************************************************************/

eststo clear
			
		local educyear = "$educyear"
		local raceyear =  "$raceyear"
		local stateyear =  "$stateyear"
		local fe = "$threeway"

				
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num /// 
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num )

			local partialr2 = e(r2)
			local fstat=e(F)
			
		eststo est1: xi: reg  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num /// 
					[aweight=cps_pop], cluster(fipstate)
			estadd local StandYr_FE = "No"
			estadd local StEdRace_FE = "No"
			estadd local YrState_FE = "No"
			estadd local YrRace_FE = "No"		
			estadd local YrEd_FE = "No"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'

			
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`fe' /// 
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe')
			
			local partialr2 = e(r2)
			local fstat=e(F)
			
		eststo est2: xi: reg  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`fe' /// 
					[aweight=cps_pop], cluster(fipstate) 
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "No"	
			estadd local YrRace_FE = "No"		
			estadd local YrEd_FE = "No"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'

		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`stateyear' /// 
					`fe'  	///
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `stateyear')					
			
			local partialr2 = e(r2)
			local fstat=e(F)
			
		eststo est3: xi: reg  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`stateyear' /// 
					`fe'  	///
					[aweight=cps_pop]				
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"
			estadd local YrRace_FE = "No"		
			estadd local YrEd_FE = "No"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'
			
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear'  `stateyear' /// 
					`fe'  /// 		
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `raceyear' `stateyear')	

			local partialr2 = e(r2)
			local fstat = e(F)
			
		eststo est4: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear'  `stateyear' /// 
					`fe'  /// 		
					[aweight=cps_pop], cluster(fipstate) 
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"	
			estadd local YrRace_FE = "Yes"		
			estadd local YrEd_FE = "No"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'
			
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' /// 
					`fe'  /// 		
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear')

			local partialr2 = e(r2)
			local fstat = e(F)

		eststo est5: xi: reg  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' /// 
					`fe'  /// 		
					[aweight=cps_pop], cluster(fipstate) 
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"	
			estadd local YrRace_FE = "Yes"		
			estadd local YrEd_FE = "Yes"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'

			
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' 	/// 
					`fe'  /// 		
					yrsedfem yrsedmale sexratio  ///				
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear' yrsedfem yrsedmale sexratio )

			local partialr2 = e(r2)
			local fstat = e(F)
			

		eststo est6: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' 	/// 
					`fe'  /// 		
					 yrsedfem yrsedmale sexratio ///				
					[aweight=cps_pop], cluster(fipstate) 
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"				
			estadd local YrRace_FE = "Yes"		
			estadd local YrEd_FE = "Yes"
			estadd local RaceState_Trend = "Yes"			
			estadd local Controls = "Yes"				
			estadd scalar fstat =  `fstat'		
			estadd scalar partialr2 = `partialr2'
	
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' 	/// 
					`fe' $racestatetrend /// 		
					yrsedfem yrsedmale sexratio  ///				
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear' yrsedfem yrsedmale sexratio  $racestatetrend)

			local partialr2 = e(r2)
			local fstat = e(F)
			
	
	
			eststo est7: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
						`educyear'  `raceyear' `stateyear' 	/// 
						`fe'  /// 		
						yrsedfem yrsedmale sexratio $racestatetrend ///				
						[aweight=cps_pop], cluster(fipstate) 
				estadd local StandYr_FE = "Yes"
				estadd local StEdRace_FE = "Yes"
				estadd local YrState_FE = "Yes"				
				estadd local YrRace_FE = "Yes"		
				estadd local YrEd_FE = "Yes"
				estadd local RaceState_Trend = "Yes"			
				estadd local Controls = "Yes"				
				estadd scalar fstat =  `fstat'		
				estadd scalar partialr2 = `partialr2'

	*** OUTPUT ESTIMATES
	
	esttab est1 est2 est3 est4 est5 est6 est7 using "${tables}/tablea4.tex", /// 
		replace keep(*wage* ) ///
		se r2 se(3) b(3)  star (* .10 ** .05 *** .01) nonotes noconstant ///
		stat( partialr2 N , /// 
		labels("Partial R-Squared" "Obs" ) /// 	
		fmt(3 0 0 0 0 0)) nomtitle numbers label ///
		postfoot("") /// 
		varlabels( , blist(lncpsshiftrelwage "\midrule \it{\underline{A: Relative}} \\ "))
	
	
/****************************************
	TABLE A4 - PANELS B AND C
*****************************************/

	foreach g in male fem {

		local educyear =  "$educyear"
		local raceyear =  "$raceyear"
		local stateyear = "$stateyear"
		local fe = "$threeway"
	
	
		xi: ivreg2 lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num [aweight=pop`g'], partial(i.race_num i.educ_num) cluster(fipstate)
		
		local partialr2 = e(r2)
		local fstat=e(F)

		eststo est1`g': xi: reg lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num [aweight=pop`g'], cluster(fipstate)
		estadd local StandYr_FE = "No"
		estadd local StEdRace_FE = "No"
		estadd local YrState_FE = "No"
		estadd local YrRace_FE = "No"		
		estadd local YrEd_FE = "No"
		estadd local RaceState_Trend = "No"			
		estadd local Controls = "No"				
		estadd scalar fstat = `fstat'
		estadd scalar partialr2 = `partialr2'
		
		xi: ivreg2 lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`fe'  /// 
					[aweight=pop`g'],  partial(i.race_num i.educ_num i.year i.fipstate `fe') cluster(fipstate)
		
		local partialr2 = e(r2)
		local fstat=e(F)
		
		eststo est2`g': xi: reg lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`fe'  /// 
					[aweight=pop`g'], cluster(fipstate)
		estadd local StandYr_FE = "Yes"
		estadd local StEdRace_FE = "Yes"
		estadd local YrState_FE = "No"
		estadd local YrRace_FE = "No"		
		estadd local YrEd_FE = "No"
		estadd local RaceState_Trend = "No"			
		estadd local Controls = "No"				
		estadd scalar fstat = `fstat'
		estadd scalar partialr2 = `partialr2'
		
	
		xi: ivreg2 lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate `stateyear' /// 
					`fe'  /// 
					[aweight=pop`g'], partial(i.race_num i.educ_num i.year i.fipstate `stateyear' `fe') cluster(fipstate)
		
		local partialr2 = e(r2)
		local fstat=e(F)

		
		eststo est3`g': xi: reg lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate `stateyear' /// 
					`fe'  /// 
					[aweight=pop`g'], cluster(fipstate)
		estadd local StandYr_FE = "Yes"
		estadd local StEdRace_FE = "Yes"
		estadd local YrState_FE = "Yes"	
		estadd local YrRace_FE = "No"		
		estadd local YrEd_FE = "No"
		estadd local RaceState_Trend = "No"			
		estadd local Controls = "No"				
		estadd scalar fstat = `fstat'
		estadd scalar partialr2 = `partialr2'
		
		xi: ivreg2 lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`stateyear' `raceyear' /// 
					`fe'  /// 
					[aweight=pop`g'], partial(i.race_num i.educ_num i.year i.fipstate `stateyear' `raceyear' `fe' )  cluster(fipstate)
		
		local partialr2 = e(r2)
		local fstat=e(F)
		
		eststo est4`g': xi: reg lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`stateyear' `raceyear' /// 
					`fe'  /// 
					[aweight=pop`g'],  cluster(fipstate)
		estadd local StandYr_FE = "Yes"
		estadd local StEdRace_FE = "Yes"
		estadd local YrState_FE = "Yes"		
		estadd local YrRace_FE = "Yes"		
		estadd local YrEd_FE = "No"			
		estadd local RaceState_Trend = "No"			
		estadd local Controls = "No"				
		estadd scalar fstat = `fstat'
		estadd scalar partialr2 = `partialr2'

		
		xi: ivreg2 lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear' `educyear' `stateyear' /// 
					`fe'  /// 
					[aweight=pop`g'], partial(i.race_num i.educ_num i.year i.fipstate `raceyear' `educyear' `stateyear'  `fe' )  cluster(fipstate)
		
		local partialr2 = e(r2)
		local fstat=e(F)
		
		eststo est5`g': xi: reg lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear' `educyear' `stateyear' /// 
					`fe'  /// 
					[aweight=pop`g'], cluster(fipstate)
		estadd local StandYr_FE = "Yes"
		estadd local StEdRace_FE = "Yes"
		estadd local YrState_FE = "Yes"	
		estadd local YrRace_FE = "Yes"					
		estadd local YrEd_FE = "Yes"			
		estadd local RaceState_Trend = "No"			
		estadd local Controls = "No"				
		estadd scalar fstat = `fstat'
		estadd scalar partialr2 = `partialr2'
		

		 xi: ivreg2 lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear' `educyear' `stateyear'  /// 
					`fe'  /// 
					yrsed`g' sexratio  ///
					[aweight=pop`g'],  partial(i.race_num i.educ_num i.year i.fipstate `raceyear' `educyear' `stateyear'  `fe' yrsed`g'  sexratio )  cluster(fipstate)
		
		local partialr2 = e(r2)
		local fstat=e(F)
		
		eststo est6`g': xi: reg lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear' `educyear' `stateyear'  /// 
					`fe'  /// 
					 yrsed`g' sexratio   ///
					[aweight=pop`g'],   cluster(fipstate)
		estadd local StandYr_FE = "Yes"
		estadd local StEdRace_FE = "Yes"
		estadd local YrState_FE = "Yes"	
		estadd local YrRace_FE = "Yes"					
		estadd local YrEd_FE = "Yes"			
		estadd local RaceState_Trend = "No"			
		estadd local Controls = "Yes"				
		estadd scalar fstat = `fstat'
		estadd scalar partialr2 = `partialr2'

		 xi: ivreg2 lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
						`raceyear' `educyear' `stateyear'  /// 
						`fe'  /// 
						yrsed`g' sexratio $racestatetrend ///
						[aweight=pop`g'],  partial(i.race_num i.educ_num i.year i.fipstate `raceyear' `educyear' `stateyear'  `fe' yrsed`g'  sexratio $racestatetrend)  cluster(fipstate)

		local partialr2 = e(r2)
		local fstat=e(F)
			
		eststo est7`g': xi: reg lncps`g'wage lncps`g'shiftwage i.race_num i.educ_num i.year i.fipstate /// 
						`raceyear' `educyear' `stateyear'  /// 
						`fe'  /// 
						 yrsed`g' sexratio $racestatetrend    ///
						[aweight=pop`g'],   cluster(fipstate)
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"	
			estadd local YrRace_FE = "Yes"					
			estadd local YrEd_FE = "Yes"			
			estadd local RaceState_Trend = "Yes"			
			estadd local Controls = "Yes"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'
						
			
	}	
	

	esttab est1fem est2fem est3fem est4fem est5fem est6fem est7fem using "${tables}/tablea4.tex", /// 
		append keep(*wage* ) ///
		se r2 se(3) b(3)  star (* .10 ** .05 *** .01) nonum nonotes noconstant ///
		stat( partialr2 N , /// 
		labels("Partial R-Squared" "Obs") /// 	
		fmt(3 0 0 0 0 0)) nomtitle label ///
		prehead("") posthead("") postfoot("") ///
		varlabels( , blist(lncpsfemshiftwage "\midrule \it{\underline{B: Female}} \\ "))
		
		
	esttab est1male est2male est3male est4male est5male est6male est7male using "${tables}/tablea4.tex", /// 
		append keep(*wage* ) ///
		se r2 se(3) b(3)  star (* .10 ** .05 *** .01) nonum nonotes noconstant ///
		stat( partialr2 N StandYr_FE StEdRace_FE YrState_FE YrRace_FE YrEd_FE   Controls RaceState_Trend, /// 
		labels(  "Partial R-Squared"  "Obs" "StandYr FE" "StEdRace FE"  "YrState FE" "YrRace FE" "YrEd FE"  "Controls" "RaceStTrend") /// 	
		fmt(3 0 0 0 0 0)) nomtitle label prehead("") posthead("") ///
		varlabels( , blist(lncpsmaleshiftwage "\midrule \it{\underline{C: Male}} \\ "))
		
	

/**************************************************
TABLE A20
***************************************************/


eststo clear

rename lncpsshiftrelwage lncpsshiftrelwage_main

foreach type in noocc noupdate main {
			
		rename  lncpsshiftrelwage_`type' lncpsshiftrelwage
		
		local educyear = "$educyear"
		local raceyear =  "$raceyear"
		local stateyear =  "$stateyear"
		local fe = "$threeway"

				
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num /// 
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num )

			local partialr2 = e(r2)
			local fstat=e(F)
			
		eststo est1: xi: reg  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num /// 
					[aweight=cps_pop], cluster(fipstate)
			estadd local StandYr_FE = "No"
			estadd local StEdRace_FE = "No"
			estadd local YrState_FE = "No"
			estadd local YrRace_FE = "No"		
			estadd local YrEd_FE = "No"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'

			
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`fe' /// 
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe')
			
			local partialr2 = e(r2)
			local fstat=e(F)
			
		eststo est2: xi: reg  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`fe' /// 
					[aweight=cps_pop], cluster(fipstate) 
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "No"	
			estadd local YrRace_FE = "No"		
			estadd local YrEd_FE = "No"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'

		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`stateyear' /// 
					`fe'  	///
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `stateyear')					
			
			local partialr2 = e(r2)
			local fstat=e(F)
			
		eststo est3: xi: reg  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`stateyear' /// 
					`fe'  	///
					[aweight=cps_pop]				
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"
			estadd local YrRace_FE = "No"		
			estadd local YrEd_FE = "No"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'
			
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear'  `stateyear' /// 
					`fe'  /// 		
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `raceyear' `stateyear')	

			local partialr2 = e(r2)
			local fstat = e(F)
			
		eststo est4: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`raceyear'  `stateyear' /// 
					`fe'  /// 		
					[aweight=cps_pop], cluster(fipstate) 
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"	
			estadd local YrRace_FE = "Yes"		
			estadd local YrEd_FE = "No"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'
			
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' /// 
					`fe'  /// 		
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear')

			local partialr2 = e(r2)
			local fstat = e(F)

		eststo est5: xi: reg  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' /// 
					`fe'  /// 		
					[aweight=cps_pop], cluster(fipstate) 
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"	
			estadd local YrRace_FE = "Yes"		
			estadd local YrEd_FE = "Yes"
			estadd local RaceState_Trend = "No"			
			estadd local Controls = "No"				
			estadd scalar fstat = `fstat'
			estadd scalar partialr2 = `partialr2'

			
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' 	/// 
					`fe'  /// 		
					yrsedfem yrsedmale sexratio  ///				
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear' yrsedfem yrsedmale sexratio )

			local partialr2 = e(r2)
			local fstat = e(F)
			

		eststo est6: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' 	/// 
					`fe'  /// 		
					 yrsedfem yrsedmale sexratio ///				
					[aweight=cps_pop], cluster(fipstate) 
			estadd local StandYr_FE = "Yes"
			estadd local StEdRace_FE = "Yes"
			estadd local YrState_FE = "Yes"				
			estadd local YrRace_FE = "Yes"		
			estadd local YrEd_FE = "Yes"
			estadd local RaceState_Trend = "Yes"			
			estadd local Controls = "Yes"				
			estadd scalar fstat =  `fstat'		
			estadd scalar partialr2 = `partialr2'
	
		xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' 	/// 
					`fe' $racestatetrend /// 		
					yrsedfem yrsedmale sexratio  ///				
					[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear' yrsedfem yrsedmale sexratio  $racestatetrend)

			local partialr2 = e(r2)
			local fstat = e(F)
			
	
	
			eststo est7: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
						`educyear'  `raceyear' `stateyear' 	/// 
						`fe'  /// 		
						yrsedfem yrsedmale sexratio $racestatetrend ///				
						[aweight=cps_pop], cluster(fipstate) 
				estadd local StandYr_FE = "Yes"
				estadd local StEdRace_FE = "Yes"
				estadd local YrState_FE = "Yes"				
				estadd local YrRace_FE = "Yes"		
				estadd local YrEd_FE = "Yes"
				estadd local RaceState_Trend = "Yes"			
				estadd local Controls = "Yes"				
				estadd scalar fstat = `fstat'		
				estadd scalar partialr2 = `partialr2'


** Table

	 if "`type'" == "noocc" ///
		esttab est1 est2 est3 est4 est5 est6 est7 using "${tables}/tablea20.tex", replace /// 
		keep(lncpsshiftrelwage ) se r2 se(3) b(3)  star (* .10 ** .05 *** .01) numbers nomtitle nonotes noconstant ///
		stat( partialr2 N, /// 
		labels( "Partial R-Squared" "Obs" ) /// 	
		fmt(3 0))  ///	
		varlabels( , blist(lncpsshiftrelwage "\midrule \it{\underline{A: Variation by demographic-industry}} \\ ")) ///
		label postfoot("") 

	 if "`type'" == "noupdate" ///
		esttab est1 est2 est3 est4 est5 est6 est7 using "${tables}/tablea20.tex", append /// 
		keep(lncpsshiftrelwage ) se r2 se(3) b(3)  star (* .10 ** .05 *** .01) numbers nomtitle nonotes noconstant ///
		stat( partialr2 N, /// 
		labels(  "Partial R-Squared"  "Obs" ) /// 	
		fmt(3 0))  ///	
		varlabels( , blist(lncpsshiftrelwage "\midrule \it{\underline{B: Variation by occupation-industry}} \\ ")) ///
		label postfoot("")  prehead("") posthead("") 	
				
		
	 if "`type'" == "main" ///
		esttab est1 est2 est3 est4 est5 est6 est7 using "${tables}/tablea20.tex", append /// 
		keep(lncpsshiftrelwage ) se r2 se(3) b(3)  star (* .10 ** .05 *** .01) nomtitle nonum nonotes noconstant ///
		stat( partialr2  N StandYr_FE StEdRace_FE YrEd_FE YrRace_FE YrState_FE Controls RaceState_Trend, /// 
		labels("Partial R-Squared" "Obs" "StandYr FE" "StEdRace FE" "YrEd FE" "YrRace FE" "YrState FE" "Controls" "RaceStTrend") /// 	
		fmt(3 0 0 0 0)) ///	
		varlabels( , blist(lncpsshiftrelwage "\midrule \it{\underline{C: Add dynamic shares}} \\ ")) ///
		label prehead("") posthead("") 	
		
		rename  lncpsshiftrelwage lncpsshiftrelwage_`type'

}

rename lncpsshiftrelwage_main lncpsshiftrelwage


/***********************************************
	TABLE A22
************************************************/


eststo clear

rename lncpsshiftrelwage lncpsshiftrelwage_main

local educyear = "$educyear"
local raceyear =  "$raceyear"
local stateyear =  "$stateyear"
local fe = "$threeway"

		
** Column 1

rename lncpsshiftrelwage_fewoccs lncpsshiftrelwage

	xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
				`educyear'  `raceyear' `stateyear' 	/// 
				`fe' $racestatetrend /// 		
				yrsedfem yrsedmale sexratio  ///				
				[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear' yrsedfem yrsedmale sexratio )

		local partialr2 = e(r2)
		local fstat = e(F)
		
		eststo: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' 	/// 
					`fe' $racestatetrend /// 		
					yrsedfem yrsedmale sexratio  ///				
					[aweight=cps_pop], cluster(fipstate) 			
			estadd scalar fstat =  `fstat'		
			estadd scalar partialr2 = `partialr2'

rename  lncpsshiftrelwage lncpsshiftrelwage_fewoccs

			
** Column 2

rename lncpsshiftrelwage_censfewoccs lncpsshiftrelwage

	xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
				`educyear'  `raceyear' `stateyear' 	/// 
				`fe' $racestatetrend  /// 		
				yrsedfem yrsedmale sexratio  ///				
				[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear' yrsedfem yrsedmale sexratio $racestatetrend )

		local partialr2 = e(r2)
		local fstat = e(F)
		
		eststo: xi: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
					`educyear'  `raceyear' `stateyear' 	/// 
					`fe'  /// 		
					yrsedfem yrsedmale sexratio  ///				
					[aweight=cps_pop], cluster(fipstate) 			
			estadd scalar fstat =  `fstat'		
			estadd scalar partialr2 = `partialr2'

rename  lncpsshiftrelwage lncpsshiftrelwage_censfewoccs
rename  lncpsshiftrelwage_main lncpsshiftrelwage

	
** Column 3

use $data/tablea22_23.dta, clear


	tab educ, g(e)
		local maxed =r(r)
	tab race, g(r)
		local maxrace =r(r)
	tab fipstate, g(s)
		local maxstate= r(r)
	tab year, g(y)
		local maxyear= r(r)
	
	//3-WAY INTERACTION: STATE*ED*RACE

	global threeway ""

	forvalues e = 1/`maxed' {
	forvalues s = 1/`maxstate' {
	forvalues r = 1/`maxrace' {
			
		global threeway $threeway e`e's`s'r`r'
		
	}
	}
	}

	
	global racestatetrend ""
	
	forvalues s = 1/`maxstate' {
	forvalues r = 1/`maxrace' {
			
		global racestatetrend $racestatetrend s`s'r`r'year
		
	}
	}
	

	global educyear ""
	
	forvalues e = 1/`maxed' {
	forvalues y = 1/`maxyear' {
			
		global educyear $educyear e`e'y`y'
		
	}
	}

	global raceyear ""
	
	forvalues r = 1/`maxrace' {
	forvalues y = 1/`maxyear' {
			
		global raceyear $raceyear r`r'y`y'
		
	}
	}	
	
	global stateyear ""
	
	forvalues s = 1/`maxstate' {
	forvalues y = 1/`maxyear' {
			
		global stateyear $stateyear s`s'y`y'
		
	}
	}		
	
	local educyear = "$educyear"
	local raceyear =  "$raceyear"
	local stateyear =  "$stateyear"
	local fe = "$threeway"

		
		
	xi: ivreg2  lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
				`educyear'  `raceyear' `stateyear' 	/// 
				`fe' $racestatetrend /// 		
				yrsedfem yrsedmale sexratio  ///				
				[aweight=cps_pop], cluster(fipstate) partial(i.race_num i.educ_num i.year i.fipstate `fe' `educyear' `raceyear'  `stateyear' yrsedfem yrsedmale sexratio $racestatetrend)

		local partialr2 = e(r2)
		local fstat = e(F)
		
	eststo: xi3: reg lncpsrelwage lncpsshiftrelwage i.race_num i.educ_num i.year i.fipstate /// 
				`educyear'  `raceyear' `stateyear' 	/// 
				`fe'  /// 		
				 yrsedfem yrsedmale sexratio i.race_num*i.fipstate*year ///				
				[aweight=cps_pop], cluster(fipstate) 			
			estadd scalar fstat =  `fstat'		
			estadd scalar partialr2 = `partialr2'
					


		 esttab using "${tables}/tablea22.tex", replace keep(lncpsshiftrelwage ) ///
		se r2 se(3) b(3)  star (* .10 ** .05 *** .01)  nonotes noconstant ///
		stat( partialr2 N, labels( "Partial R-Squared" "Obs") fmt(3 0 0 0 0)) ///
		mtitle("21 Occs" "+ Census" "+ Col. Groups") label 			


